package com.javaweb.service.impl;

import com.javaweb.common.BusinessConstants;
import com.javaweb.dao.ArticleDao;
import com.javaweb.dao.UserDao;
import com.javaweb.model.Article;
import com.javaweb.service.ArticleService;
import com.javaweb.service.dto.ArticleUpdateDto;
import com.javaweb.service.dto.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/10/16 9:47
 */

@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private UserDao userDao ;
    @Autowired
    private ArticleDao articleDao ;

    public void method() {
        this.publishArticle("1","1") ;
    }


    @Transactional
//            (
//            propagation = Propagation.REQUIRED,
//            isolation = Isolation.DEFAULT,
//            timeout = -1,
//            readOnly = true,
//            rollbackFor = Throwable.class
//    )
    @Override
    public int publishArticle(String userId, String articleId) {

        Integer ret =  articleDao.updateStatus(Integer.valueOf(articleId),
                               BusinessConstants.ARTICLE_PUBLISHED) ;
        if (ret > 0) {
            ret = userDao.updateScore(Integer.valueOf(userId),
                    BusinessConstants.ARTICLE_SCORE) ;
        } else {
            log.error("publish article : update status failed." ) ;
            return 0 ;
        }
        try {
            if (true) {
                throw new RuntimeException("exception.");
            }
        } catch (Exception e) {
            log.error(e.getMessage()) ;
        }
        log.info("publish article successfully." ) ;
        return ret;
    }
    @Override

   public PageResult queryByArticleCondition(Article articleCond, Integer pageNum, Integer pageSize){
        List<Article> articles = articleDao.selectByUserId(articleCond.getUserId());
        int totalRecords = articles.size();

        PageResult pageResult = new PageResult();
        // 设置用户列表
        pageResult.setTotal(totalRecords); // 设置总用户数
        pageResult.setPageNum(pageNum);       // 设置当前页码
        pageResult.setPageSize(pageSize);     // 设置每页的记录数

        return pageResult;
    }
    @Override
    public Article queryById(int articleId) {

        return (Article) articleDao.selectByUserId(articleId).get(0);
    }
    @Override
    public Article insertArticle(Article article) {
        // 插入文章
          articleDao.insert(article);


            return article;

    }
    @Override
    @Transactional // 保证事务性
    public int publishArticlesByBatch(String[] articleIds, String userId) {
        int successCount = 0;
        for (String articleId : articleIds) {
            successCount += publishArticle(articleId, userId);
        }
        return successCount == articleIds.length ? 1 : 0;
    }
@Override
    @Transactional // 保证事务性
    public int updateArticle(ArticleUpdateDto articleVo) {
        // 更新文章内容
        int updateResult = articleDao.updateTitle(Integer.valueOf(articleVo.getId()),articleVo.getTitle());

        if (updateResult > 0) {
            return 1; // 更新成功
        } else {
            return 0; // 更新失败
        }
    }
    @Override
    @Transactional // 保证事务性
    public int removeArticle(String articleId, String userId) {
        // Step 1: 删除文章
        int deleteResult = articleDao.deleteById(Integer.valueOf(articleId));

        if (deleteResult > 0) {
            // Step 2: 更新用户的活动积分
            int updatePoints = userDao.updateScore(Integer.valueOf(userId), -10); // 假设删除文章扣减10积分

            if (updatePoints > 0) {
                return 1; // 删除成功
            } else {
                throw new RuntimeException("更新用户积分失败");
            }
        }
        return 0; // 删除失败
    }


}
